﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Globalization;
using Mabalacat.CPS.BusinessLogic.Services;
using Mabalacat.CPS.BusinessLogic.Services.Interfaces;

namespace Web.Utils
{
    public static class DropDownHelper
    {
        
        public static void ConstructDateDropdowns(DropDownList month,DropDownList day ,DropDownList year, DateTime? selectedDate)
        {

            for (int i = 0; i < 12; i++)
            {
                var iDay = i + 1;
                month.Items.Add(new ListItem 
                {
                    Value = iDay.ToString(),
                    Text = CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames[i].Substring(0,3),
                    Selected = selectedDate.HasValue && selectedDate.Value.Month == iDay
                });
                
            }

            for (int i = 1; i <= 31; i++)
            {
                day.Items.Add(new ListItem
                {
                    Value = i.ToString(),
                    Text = i.ToString().PadLeft(2,'0'),
                    Selected = selectedDate.HasValue && selectedDate.Value.Day == i
                });
            }

            for (int i = DateTime.Now.Year - 65; i < DateTime.Now.Year + 1; i++)
            {
                year.Items.Add(new ListItem
                {
                    Value = i.ToString(),
                    Text = i.ToString(),
                    Selected = selectedDate.HasValue && selectedDate.Value.Year == i
                });
            }

        }

        #region ExtensionMethods
        public static void ToPositionSelectList(this DropDownList dropdown, int selectedValue)
        {
            var _positionService = new PositionService();

            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });
            dropdown.Items.AddRange(
                  _positionService.GetAll()
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = x.Code,
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }
        public static void ToDivisionSelectList(this DropDownList dropdown, int selectedValue)
        {
            var _divisionService = new DivisionService();

            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });
            dropdown.Items.AddRange(
                  _divisionService.GetAll()
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = x.Code,
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }
        #endregion
    }
}